package com.example.musiccommunity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.example.musiccommunity.bean.Notice;

import org.json.JSONException;
import org.json.JSONObject;

public class NoticeDetail extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView info;
    private TextView content;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice_detail);

        toolbar = findViewById(R.id.toolbar);
        info = findViewById(R.id.info);
        content = findViewById(R.id.content);

        toolbar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        reqString();


    }
    void reqString(){
        String url=MainActivity.ip+"/notice/getOne?id="+getIntent().getStringExtra("id");
        System.out.println(url);
        StringRequest stringRequest=new StringRequest(Request.Method.POST,url, new com.android.volley.Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                if(s==null || s.equals("")){
                    Toast.makeText(getBaseContext(), "获取数据失败", Toast.LENGTH_SHORT).show();
                }
                else{
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(s);
                        String id = jsonObject.get("id").toString();
                        Notice notice=new Notice(id,
                                jsonObject.get("title").toString(),
                                jsonObject.get("txt").toString(),
                                jsonObject.get("time").toString() );
                        String str="";
                        str+=""+notice.getTitle()+"\n\n";
                        str+=""+notice.getTxt()+"";
                        info.setText(str);
                        content.setText(notice.getTime());

                    } catch (JSONException e) {
                        System.out.println(e.toString());
                        throw new RuntimeException(e);
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Log.d("--------",volleyError.toString());
                Toast.makeText(getBaseContext(), "获取数据失败", Toast.LENGTH_SHORT).show();
            }
        });//字符串
        MainActivity.requestQueue.add(stringRequest);

    }
}